///////////////////////////////////////////////////
// Election Administration Polarization
// By State RD Table
///////////////////////////////////////////////////

gl path "~/Library/CloudStorage/GoogleDrive-danmckinleythompson@gmail.com/My Drive/ElecAdminPolarization/How_Partisan_Is_Local_Election_Admin_Replication"


* Bring in the presidential election analysis data
use "$path/analysis_data/rd_election_analysis_data.dta", clear
keep if office=="pres"


* Run separate regressions by states with at least 50 competitive races


* Spec 1, Alabama
rdrobust r_oos_state_year_vs_dem rv if state== "AL", vce(cluster election_id)
rdtab r_oos_state_year_vs_dem treat rv election_id if state== "AL", est_command("rdrobust") new("B") tau(0.02) alpha(0.05) onesided

* Spec 2, Colorado
rdrobust r_oos_state_year_vs_dem rv if state== "CO", vce(cluster election_id)
rdtab r_oos_state_year_vs_dem treat rv election_id if state== "CO", est_command("rdrobust") old("B") tau(0.02) alpha(0.05) onesided

* Spec 3, Florida
rdrobust r_oos_state_year_vs_dem rv if state== "FL", vce(cluster election_id)
rdtab r_oos_state_year_vs_dem treat rv election_id if state== "FL", est_command("rdrobust") old("B") tau(0.02) alpha(0.05) onesided

* Spec 4, Iowa
rdrobust r_oos_state_year_vs_dem rv if state== "IA", vce(cluster election_id)
rdtab r_oos_state_year_vs_dem treat rv election_id if state== "IA", est_command("rdrobust") old("B") tau(0.02) alpha(0.05) onesided

* Spec 5, Illinois
rdrobust r_oos_state_year_vs_dem rv if state== "IL", vce(cluster election_id)
rdtab r_oos_state_year_vs_dem treat rv election_id if state== "IL", est_command("rdrobust") old("B") tau(0.02) alpha(0.05) onesided

* Spec 6, Indiana
rdrobust r_oos_state_year_vs_dem rv if state== "IN", vce(cluster election_id)
rdtab r_oos_state_year_vs_dem treat rv election_id if state== "IN", est_command("rdrobust") old("B") tau(0.02) alpha(0.05) onesided

* Spec 7, Kentucky
rdrobust r_oos_state_year_vs_dem rv if state== "KY", vce(cluster election_id)
rdtab r_oos_state_year_vs_dem treat rv election_id if state== "KY", est_command("rdrobust") old("B") tau(0.02) alpha(0.05) onesided

* Spec 8, Texas
rdrobust r_oos_state_year_vs_dem rv if state== "TX", vce(cluster election_id)
rdtab r_oos_state_year_vs_dem treat rv election_id if state== "TX", est_command("rdrobust") old("B") tau(0.02) alpha(0.05) onesided


qui {
* Build the memo/paper table
cap log close
set linesize 255
log using "$path/output/rd_table_by_state.tex", text replace
noi di "\begin{table}[ht]"
noi di "\centering"
noi di "\caption{\textbf{Effect of Democratic Election Officials on Democratic Presidential Vote Share Across States.}"
noi di "\label{tab:rd_table_by_state}}"
noi di "\begin{tabular}{lcccccccc}"
noi di "\toprule \toprule"
noi di " & \multicolumn{8}{c}{Dem Pres Vote Share}\\"
noi di " & Alabama & Colorado & Florida & Iowa & Illinois & Indiana & Kentucky & Texas \\ "
noi di " & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\"
noi di "\midrule"
noi di "Dem Elec Official & " %4.3f B[1,1] " & " %4.3f B[1,2] " & " %4.3f B[1,3] " & " %4.3f B[1,4] " & " %4.3f B[1,5] " & " %4.3f B[1,6] " & " %4.3f B[1,7] " & " %4.3f B[1,8] " \\"
noi di " & (" %4.3f B[2,1] ") & (" %4.3f B[2,2] ") & (" %4.3f B[2,3] ") & (" %4.3f B[2,4] ") & (" %4.3f B[2,5] ") & (" %4.3f B[2,6] ") & (" %4.3f B[2,7] ") & (" %4.3f B[2,8] ")  \\[2mm]"
noi di "\midrule"
noi di "N & " %4.0f B[3,1] " & " %4.0f B[3,2] " & " %4.0f B[3,3] " & " %4.0f B[3,4] " & " %4.0f B[3,5] " & " %4.0f B[3,6] " & " %4.0f B[3,7] " & " %4.0f B[3,8] " \\ "
noi di "Bandwidth & " %4.2f B[5,1] " & " %4.2f B[5,2] " & " %4.2f B[5,3] " & " %4.2f B[5,4] " & " %4.2f B[5,5] " & " %4.2f B[5,6] " & " %4.2f B[5,7] " & " %4.2f B[5,8] " \\ "
noi di "BW Selection & CCT & CCT & CCT & CCT & CCT & CCT & CCT & CCT \\"
noi di "Kernel & Tri & Tri & Tri & Tri & Tri & Tri & Tri & Tri \\"
noi di "\bottomrule \bottomrule"
noi di "\multicolumn{9}{p{1.05\textwidth}}{\footnotesize Robust standard errors in parentheses. "
noi di "The outcome is first regressed on a state- and year-specific lag using all counties including those for which clerk election "
noi di "results are not available. The regression discontinuity is estimated using the residuals from that regression. "
noi di "The bandwidth row reports the number of maximum clerk win margin allowed for inclusion in each specificaiton. "
noi di "CCT refers to \cite{calonico2014robust} bandwidth selection procedure. "
noi di "Tri means the specification uses a triangular kernel. }"
noi di "\end{tabular}"
noi di "\end{table}"
log off
}
